﻿using IcsSEMCtrl.Service.Logger;
using IcsSEMCtrl.Util.Client;
using Lifter.Device;
using Lifter.Device.Unit;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TKT.Lifter.CIM.Lifter.Gui.Monitor.Popups
{
    public partial class VehicleManualTransfer : UIEditForm
    {
        LifterSEM lifterSEM = (LifterSEM)Naming.GetDeviceManager().GetLifter();
        public VehicleManualTransfer()
        {
            InitializeComponent();
            this.LoadVehicleSourceDestData();
        }
        /// <summary>
        /// 加载起始地，目的地下拉框数据
        /// </summary>
        private void LoadVehicleSourceDestData()
        {
            //获取轿厢数据
            List<string> vehicles = new List<string>();
            foreach (Vehicle vehicle in lifterSEM.Vehicles.Values.ToList())
            {
                vehicles.Add(vehicle.HostID);
            }

            List<ConveyorBuffer> conveyorBuffers = lifterSEM.ConveyorBuffers.Values.ToList();
            List<string> conveyorSource = new List<string>();
            List<string> conveyorDest = new List<string>();
            
            foreach (ConveyorBuffer conveyorBuffer in conveyorBuffers)
            {
                //获取送料CV
                if (ConveyorBuffer.Constants.CVUNLOAD.Equals(conveyorBuffer.Cvtype))
                {
                    conveyorSource.Add(conveyorBuffer.HostID);
                }
                //获取接料CV
                if (ConveyorBuffer.Constants.CVLOAD.Equals(conveyorBuffer.Cvtype))
                {
                    conveyorDest.Add(conveyorBuffer.HostID);
                }
            }
            conveyorSource.AddRange(vehicles);
            conveyorDest.AddRange(vehicles);
            this.cbVehicleSource.Items.AddRange(conveyorSource.ToArray());
            this.cbVehicleDest.Items.AddRange(conveyorDest.ToArray());
        }

        public void CreateManualMoveCommand()
        {
            //1.判断轿厢是否在ready状态，判断轿厢状态是否为Down
            //2.判断目的地是否有物料
            //3.判断起始地是否有物料

            string transferSource = this.cbVehicleSource.Text;
            string transferDest = this.cbVehicleDest.Text;

            if (string.IsNullOrEmpty(transferSource))
            {
                ShowWarningDialog("请选择轿厢搬送起始地");
                return;
            }
            if (string.IsNullOrEmpty(transferDest))
            {
                ShowWarningDialog("请选择轿厢搬送目的地");
                return;
            }

            //创建轿厢搬送指令

            //1.判断轿厢是否能创建Move指令
            bool flag = this.lifterSEM.ShuttleMaster.JudgeDoManualCommand();
            if (flag)
            {
                bool returnCode = this.lifterSEM.ShuttleMaster.ManualCreateVehicelTransferCommand(transferSource, transferDest);
                if (returnCode)
                {
                    ShowSuccessDialog("手动创建轿厢搬送指令成功");
                    LogHelper.operationLogger($"用户{lifterSEM.UserName}进行了手动创建轿厢搬送指令操作。");
                }
                else
                {
                    ShowWarningDialog("手动创建轿厢搬送指令失败");
                }
            }
            else
            {
                ShowWarningDialog("创建失败,设备状态为Down,或者设备运行模式不为Auto模式，或者轿厢状态为Down,或者轿厢搬送状态不为Ready状态,请确认！！");
                LogHelper.loggerInfo("创建失败,设备状态为Down,或者设备运行模式不为Auto模式，或者轿厢状态为Down,或者轿厢搬送状态不为Ready状态");
            }
        }
    }
}
